package com.yiren.manager.service.impl.cost;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yiren.core.exception.MyException;
import com.yiren.core.service.GenericServiceImpl;
import com.yiren.core.utils.FavoriteSet;
import com.yiren.entity.YrElderArrearage;
import com.yiren.entity.YrElderPayRecord;
import com.yiren.entity.YrElderWallet;
import com.yiren.manager.repository.cost.YrElderArrearageRepository;
import com.yiren.manager.repository.cost.YrElderPayRecordRepository;
import com.yiren.manager.repository.elder.YrElderWalletRepository;
import com.yiren.manager.service.iface.cost.YrElderArrearageService;

@Service
public class YrElderArrearageServiceImpl extends GenericServiceImpl<YrElderArrearage, String> implements YrElderArrearageService{

	@Autowired
	private YrElderWalletRepository elderWalletRepository;
	
	@Autowired
	private YrElderPayRecordRepository elderPayRecordRepository;
	
	@Autowired
	public YrElderArrearageServiceImpl(YrElderArrearageRepository repository) {
		super(repository);
	}

	@Transactional
	public void updateBetch(List<YrElderArrearage> list) throws MyException {
		for(YrElderArrearage entity : list){
			this.repository.save(entity);
		}
	}

	@Transactional
	public void executePayCost(FavoriteSet favoriteSet) {
		List<YrElderArrearage> elderArrearages = favoriteSet.getFavorite("elderArrearages", List.class);
		YrElderWallet elderWallet = favoriteSet.getFavorite("elderWallet", YrElderWallet.class);
		List<YrElderPayRecord> elderPayRecords = favoriteSet.getFavorite("elderPayRecords",List.class);
		
		for(YrElderArrearage elderArrearage : elderArrearages){
			this.repository.save(elderArrearage);
		}
		if(null!=elderWallet){
			elderWalletRepository.save(elderWallet);
		}
		for(YrElderPayRecord elderPayRecord : elderPayRecords){
			elderPayRecordRepository.save(elderPayRecord);
		}
	}

}
